package com.ruoyi.cms.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CVideoMapper;
import com.ruoyi.cms.domain.CVideo;
import com.ruoyi.cms.service.ICVideoService;

/**
 * 视频管理Service业务层处理
 * 
 * @author Choi
 * @date 2021-08-23
 */
@Service
public class CVideoServiceImpl implements ICVideoService 
{
    @Autowired
    private CVideoMapper cVideoMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询视频管理
     * 
     * @param id 视频管理ID
     * @return 视频管理
     */
    @Override
    public CVideo selectCVideoById(Long id)
    {
        return cVideoMapper.selectCVideoById(id);
    }

    /**
     * 查询视频管理列表
     * 
     * @param cVideo 视频管理
     * @return 视频管理
     */
    @Override
    public List<CVideo> selectCVideoList(CVideo cVideo)
    {
        cVideo.setUserId(this.getUserId());
        return cVideoMapper.selectCVideoList(cVideo);
    }

    /**
     * 新增视频管理
     * 
     * @param cVideo 视频管理
     * @return 结果
     */
    @Override
    public int insertCVideo(CVideo cVideo)
    {
        cVideo.setUserId(this.getUserId());
        cVideo.setCreateTime(DateUtils.getNowDate());
        return cVideoMapper.insertCVideo(cVideo);
    }

    /**
     * 修改视频管理
     * 
     * @param cVideo 视频管理
     * @return 结果
     */
    @Override
    public int updateCVideo(CVideo cVideo)
    {
        cVideo.setUpdateTime(DateUtils.getNowDate());
        return cVideoMapper.updateCVideo(cVideo);
    }

    /**
     * 批量删除视频管理
     * 
     * @param ids 需要删除的视频管理ID
     * @return 结果
     */
    @Override
    public int deleteCVideoByIds(Long[] ids)
    {
        return cVideoMapper.deleteCVideoByIds(ids);
    }

    /**
     * 删除视频管理信息
     * 
     * @param id 视频管理ID
     * @return 结果
     */
    @Override
    public int deleteCVideoById(Long id)
    {
        return cVideoMapper.deleteCVideoById(id);
    }

    private SysUser getUser() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return null;
        }
        SysUser user = loginUser.getUser();
        return user;
    }

    private Long getUserId() {
        SysUser user = this.getUser();
        return user == null ? 0 : user.getUserId();
    }
}
